package task

import (
	"github.com/gin-gonic/gin"
	"message-center/pkg/gdingrobot"
	"message-center/pkg/gerrno"
	"message-center/pkg/ginput"
	"net/http"
)

/**
 * @api {POST} /api/v1/send/task/ding 钉钉机器人消息
 * @apiVersion 1.0.0
 * @apiName  health
 * @apiGroup  task
 * @apiDescription 发送钉钉机器人消息
 * @apiExample {curl} Example usage:
 * curl -X POST -H "Content-Type:application/json" -d '{"webhook":"钉钉机器人url token", "content": "hello"}' -i http://localhost:9090/api/v1/send/task/ding
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "code": 200,
 *       "msg": "ok",
 *       "data": "发送成功！"
 *     }
 */
func SendDing(c *gin.Context) {
	var (
		rsp       = ginput.Gin{C: c}
		reqData   = make(map[string]interface{})
		atMobiles = []string{}
		isAtAll   = false
	)

	httpCode, errCode := rsp.ParamParse(&reqData)

	if httpCode != http.StatusOK {
		rsp.SendJSON(httpCode, errCode, nil)
		return
	}

	robot := gdingrobot.NewDingRobot(reqData["webhook"].(string))
	content := reqData["content"].(string)

	err := robot.SendText(content, atMobiles, isAtAll)
	if err != nil {
		rsp.SendJSON(http.StatusInternalServerError, gerrno.ERROR, err)
		return
	}

	res := "发送成功！"
	rsp.SendJSON(http.StatusOK, gerrno.SUCCESS, res)
}
